package com.py.credit.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.py.credit.common.ServiceException;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.constant.ApiPath;
import com.py.credit.constant.ResourcePath;
import com.py.credit.dao.ModelBaseDao;
import com.py.credit.dao.ModelTestDao;
import com.py.credit.entity.*;
import com.py.credit.service.*;
import com.py.credit.utils.*;
import com.py.framework.core.common.Page;
import com.py.framework.core.helper.UrlHelper;
import com.py.framework.core.http.HttpAgent;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

@Service
public class ModelTestServiceImpl implements ModelTestService {

    @Autowired
    private ModelTestDao modelTestDao;

    @Autowired
    private ModelBaseDao modelBaseDao;

    @Autowired
    private XypjRemoteBaseService xypjRemoteBaseService;
    
    @Autowired
    private ReportGetService reportGetService;

    @Autowired
    CreditReportCustomService customService;

    @Autowired
    private UrlHelper urlHelper;

    @Override
    public List<Map<String, Object>> getByConditionForPage(List<String> columns, String[] additionalColumns,
                                                           String conditionStr, Page page, String mainType,String userId) {
        List<String> conditions = SQLBuilder.createConditionSql(conditionStr);
        List<Map<String, Object>> batchLogs = modelTestDao.selectByConditionForPage(columns, additionalColumns,
                conditions, page, mainType,userId);

        return batchLogs;
    }

    @Override
    public void addPcInfo(ModelTestPc modelTestPc, List listTybm) {
        long timeB = System.currentTimeMillis();
        modelTestDao.addPc(modelTestPc);
        modelTestDao.initPcDetailByEvalInfo(modelTestPc.getId(), modelTestPc.getModelId(), listTybm);
        long timeA = System.currentTimeMillis();
        modelTestDao.updatePcExecuteTime(String.valueOf(timeA - timeB), modelTestPc.getId());

    }

    @Override
    public List<ModelTestChartPoint> toNormalDistributionChart(String pcId, String rate) {
        // TODO Auto-generated method stub
        return modelTestDao.toNormalDistributionChart(pcId, rate);
    }

    @Override
    public List<ModelTestRankForPiePoint> toRankForPieChart(String pcId) {
        // TODO Auto-generated method stub
        return modelTestDao.toRankForPieChart(pcId);
    }

    @Override
    public List<ModelTestScoreForBarPoint> toScoreForBarChart(String pcId) {
        // TODO Auto-generated method stub
        return modelTestDao.toScoreForBarChart(pcId);
    }

    @Override
    public List<ModelDetail> selectPcDetailByConditionForPage(List<String> columns, String[] additionalColumns,
                                                              String conditionStr, Page page, String mainType) {
        List<String> conditions = SQLBuilder.createConditionSql(conditionStr);
        List<ModelDetail> batchLogs = modelTestDao.selectPcDetailByConditionForPage(columns, additionalColumns,
                conditions, page, mainType);

        return batchLogs;
    }

    @Override
    public void autoScore(ModelTestPc modelTestPc,String scoreType, String list,String pdfForXypjPagePathYz,String absolutepath,String googlePath,String pdfJsPath) {
    	try {
			
		
        long start = System.currentTimeMillis();
        modelTestDao.addPc(modelTestPc);
        List<String> l = new ArrayList<String>();
        
        long countAll=0;
        if(StringUtils.isBlank(list)){
          countAll = xypjRemoteBaseService.selectCountAll(modelTestPc.getMainType());
        }else{
          countAll  = Arrays.asList(list.split(",")).size();
        }
        
        //long countAll = xypjRemoteBaseService.selectCountAll(modelTestPc.getMainType());
        
        int startP = 0;
        int size = 1000;
        for (int m = 0; m >= 0; m++) {
            if (StringUtils.isBlank(list)) {
                l = xypjRemoteBaseService.selectTybmByLimit(modelTestPc.getMainType(), startP * 1000, size);
                list = l.toString().replace("[", "").replace("]", "").replace(" ", "");
                startP += 1;
            } else {
                l = Arrays.asList(list.split(","));
                startP += 1;
                countAll = l.size();
            }
            modelTestDao.initPcDetailByEvalInfo(modelTestPc.getId(), modelTestPc.getModelId(), l);
            double weightAll = 0;
            List<Map<String, String>> listZz = modelTestDao.selectZzIndexesByModel(modelTestPc.getModelId());
            for (int i = 0; i < listZz.size(); i++) {
                String indexCatModelId = listZz.get(i).get("indexCatModelId");
                String inputType = listZz.get(i).get("inputType");
                double weightIndex = Double.parseDouble(listZz.get(i).get("weight"));
                String pId = listZz.get(i).get("pId");
                weightIndex = weightIndex / 100;
                weightIndex = new BigDecimal(weightIndex).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                double weight = getWeight(weightIndex, modelTestPc.getModelId(), pId, "category");
                if(scoreType.equals("2")){
                	weight=1.0;
                }
                weightAll += weight;
                String type = listZz.get(i).get("type");
                List<Map> rulesSfyzz = modelBaseDao.getRuleListByIndexModelCatForZz(listZz.get(i).get("indexCatModelId"),
                        "SFYZZ");
                List<Map> rulesZzsfdq = modelBaseDao.getRuleListByIndexModelCatForZz(listZz.get(i).get("indexCatModelId"),
                        "ZZSFDQ");
                double rate = 1;
                if (rulesSfyzz != null && rulesSfyzz.size() > 0 && rulesZzsfdq != null && rulesZzsfdq.size() > 0) {
                    rate = 0.5;
                }
                double scoreTrue = 0;
                double scoreFalse = 0;
                if (rulesSfyzz != null && rulesSfyzz.size() > 0) {
                    for (int j = 0; j < rulesSfyzz.size(); j++) {
                        if (rulesSfyzz.get(j).get("start").equals("1")) {
                            scoreTrue = Double.parseDouble(String.valueOf(rulesSfyzz.get(j).get("score")));
                            continue;
                        }
                        if (rulesSfyzz.get(j).get("start").equals("2")) {
                            scoreFalse = Double.parseDouble(String.valueOf(rulesSfyzz.get(j).get("score")));
                            continue;
                        }
                    }
                }

                Map<String, Object> cardsMap = new HashMap<>();
                cardsMap.put("type", inputType);
                cardsMap.put("rate", rate);
                cardsMap.put("pcId", modelTestPc.getId());
                cardsMap.put("indexCatModelId", indexCatModelId);
                cardsMap.put("weight", weight);
                List<String> listSelected = new ArrayList<String>();
                if (!StringUtils.isBlank(list)) {
                    listSelected = Arrays.asList(list.split(","));
                }
                cardsMap.put("listSelected", listSelected);

                cardsMap.put("scoreTrue", scoreTrue);
                cardsMap.put("scoreFalse", scoreFalse);
                modelTestDao.insertCardsSfyzzAll(cardsMap);
                if (rulesZzsfdq != null && rulesZzsfdq.size() > 0) {
                    for (int j = 0; j < rulesZzsfdq.size(); j++) {
                        if (rulesZzsfdq.get(j).get("start").equals("1")) {
                            scoreTrue = Double.parseDouble(String.valueOf(rulesZzsfdq.get(j).get("score")));
                            continue;
                        }
                        if (rulesZzsfdq.get(j).get("start").equals("2")) {
                            scoreFalse = Double.parseDouble(String.valueOf(rulesZzsfdq.get(j).get("score")));
                            continue;
                        }
                    }
                }

                modelTestDao.insertCardsZzsfdqAll(cardsMap);
            }
            List<Map<String, String>> listSql = modelTestDao.selectSqlIndexesByModel(modelTestPc.getModelId());
            for (int i = 0; i < listSql.size(); i++) {
                String sql = listSql.get(i).get("callContent");
                String indexCatModelId = listSql.get(i).get("indexCatModelId");
                String inputType = listSql.get(i).get("inputType");
                double weightIndex = Double.parseDouble(listSql.get(i).get("weight"));
                String pId = listSql.get(i).get("pId");
                weightIndex = weightIndex / 100;
                weightIndex = new BigDecimal(weightIndex).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                double weight = getWeight(weightIndex, modelTestPc.getModelId(), pId, "category");
                if(scoreType.equals("2")){
                	weight=1.0;
                }
                if(scoreType.equals("2")){
                	weight=1.0;
                }
                weightAll += weight;
                List<Map> listV = xypjRemoteBaseService.autoScoreByIndexSql(modelTestPc.getMainType(), sql, list, "");
                if (listV != null && listV.size() > 0) {
                    modelTestDao.initIndexInfo(modelTestPc.getId(), indexCatModelId, listV);
                }
                if (StringUtils.isNotBlank(inputType) && "2".equals(inputType)) {
                    modelTestDao.updateIndexScoreForDict(modelTestPc.getId(), indexCatModelId, weight);
                } else {
                    modelTestDao.updateIndexScore(modelTestPc.getId(), indexCatModelId, weight);
                }

            }

            // 财务指标
            List<Map<String, String>> listFinance = modelTestDao.selectFinanceIndexesByModel(modelTestPc.getModelId());
            for (int i = 0; i < listFinance.size(); i++) {
                String field = listFinance.get(i).get("treeElem");
                String indexCatModelId = listFinance.get(i).get("indexCatModelId");
                String inputType = listFinance.get(i).get("inputType");
                double weightIndex = Double.parseDouble(listFinance.get(i).get("weight"));
                String pId = listFinance.get(i).get("pId");
                weightIndex = weightIndex / 100;
                weightIndex = new BigDecimal(weightIndex).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                double weight = getWeight(weightIndex, modelTestPc.getModelId(), pId, "category");
                weightAll += weight;
                Map<String, Object> finanxceMap = new HashMap<>();
                finanxceMap.put("field", field);
                finanxceMap.put("pcId", modelTestPc.getId());
                finanxceMap.put("indexCatModelId", indexCatModelId);
                finanxceMap.put("weight", weight);
                finanxceMap.put("listSelected", l);
                modelTestDao.insertFinance(finanxceMap);
                if (StringUtils.isNotBlank(inputType) && "2".equals(inputType)) {
                    modelTestDao.updateIndexScoreForDict(modelTestPc.getId(), indexCatModelId, weight);
                } else {
                    modelTestDao.updateIndexScore(modelTestPc.getId(), indexCatModelId, weight);
                }

            }
            double leftWeight = 1 - weightAll;
            if(scoreType.equals("2")){
            	leftWeight=0;
            }
            List<Map> listAll = xypjRemoteBaseService.getAllEnterprise(modelTestPc.getMainType(), list, "");
            list = "";
            if (listAll.size() > 0) {
                modelTestDao.initPcDetailByModelTest(modelTestPc.getId(), listAll);
            }
            modelTestDao.updatePcDetailForScore(modelTestPc.getId(), modelTestPc.getInitIndexScore(), leftWeight);
            modelTestDao.updatePcProcess(startP, countAll, modelTestPc.getId());

            if (l.size() < 1000) {
                break;
            }
        }
        long end = System.currentTimeMillis();
        //modelTestDao.deleForUserReport(modelTestPc.getId());
        modelTestDao.updateBgbhByPc(modelTestPc.getId());
        modelTestDao.updatePcDetailRank(modelTestPc.getId(), modelTestPc.getModelId());
        modelTestDao.updatePcExecuteTime(String.valueOf(end - start), modelTestPc.getId());
    	} catch (Exception e) {
    		ExceptionLogger.error("模型测算自动出分",e.getMessage(),e);
		}
    	 String pcId = modelTestPc.getId();
         String pch = modelTestDao.getpchByPcid(pcId);
    	 List<Map<String, Object>> rep = new ArrayList<>();
         String table = "";
         String tablePc = "";
         try {
             long start = System.currentTimeMillis();
             List<Map<String, String>> listE = new ArrayList<Map<String, String>>();
             if (pch.indexOf("M") > -1) {
                 table = "xypj_model_test_detail";
                 tablePc = "xypj_model_test_pc";
             } else {
                 table = "xypj_score_running_detail";
                 tablePc = "xypj_model_test_pc";
             }
             listE = reportGetService.selectEnterpriseByPc(table, pcId);
             reportGetService.updateReportStatus(tablePc, "1", pcId);
             reportGetService.updateReportStatusBypcId(table, "1", pcId);

             for (int i = 0; i < listE.size(); i++) {
                 //使用Freemarker模板生成PDF文件
                 try {
                     String id = listE.get(i).get("id");
                     String qymc = listE.get(i).get("mainName");
                     String tybm = listE.get(i).get("tybm");
                     reportExportYz(qymc, tybm, pch, pcId, id);
                 } catch (Exception ex) {
                     ExceptionLogger.error("[ModelTestServiceImpl.autoScore] reportExportYz error: ", ex.getMessage(), ex);
                 }
             }

             String url = pdfForXypjPagePathYz ;
//             for (int i = 0; i < listE.size(); i++) {
//                 Map<String, String> pcDetail = reportGetService.selectPcDetail(table, listE.get(i).get("tybm"), pcId);
//                 String bgbh = pcDetail.get("bgbh");
//                 String score = String.valueOf(pcDetail.get("score"));
//                 String rank = pcDetail.get("rank");
//                 String evalInfo = pcDetail.get("bgbh");
//                 url = url + "?tybm=" + listE.get(i).get("tybm") + "&pcId=" + pcId + "&bgbh=" + bgbh + "&score=" + score + "&rank=" + rank + "&evalInfo=" + evalInfo+"&qymc=" + listE.get(i).get("mainName") ;
//                 boolean googleConvent = HtmlToPdfUtils.googleConventSync(pdfJsPath,
//                         url,
//                         absolutepath + ResourcePath.CREDIT_REPORT_DIR + pch, listE.get(i).get("mainName") + ".pdf", null, false,
//                         googlePath);
//             }
//             long end = System.currentTimeMillis();
//             System.out.println(end - start);
             FileUtils.compressedFile(absolutepath + ResourcePath.CREDIT_REPORT_DIR + pch, absolutepath + ResourcePath.CREDIT_REPORT_DIR_ZIP);
//             long stop = System.currentTimeMillis();
//             System.out.println(stop - end);
             reportGetService.updateReportStatus(tablePc, "2", pcId);
             reportGetService.updateReportStatusBypcId(table, "2", pcId);
         } catch (Exception e) {
             reportGetService.updateReportStatus(tablePc, "3", pcId);
             reportGetService.updateReportStatusBypcId(table, "3", pcId);
         }

    }

    private void reportExportYz (String qymc, String tybm, String pch, String pcId, String id) {
        String url = urlHelper.getPlatformServerUrl() + "/creditreport/reportExportYz.json";
        Map<String, String> params = new HashMap<>(2);
        params.put("qymc",qymc);
        params.put("tybm",tybm);
        params.put("pch",pch);
        params.put("pcId",pcId);
        params.put("id",id);
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, null);
        } catch (Exception e) {
            RestServiceResult<List<Object>> userData = new RestServiceResult<>();
            userData.setCode(RestServiceResult.CODE_UNKNOW);
            userData.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error(
                    "远程服务-获取当前用户信息调用异常！(XypjRemoteBaseService#getRolesByUserId) URL:" + url + "Result:" + result,
                    e.getMessage(), e);
        }
    }

    @Transactional
    @Override
    public void autoScoreUpload(ModelTestPc modelTestPc,String scoreType, String batchId,String pdfForXypjPagePathYz,String absolutepath,String googlePath,String pdfJsPath) {
        long start = System.currentTimeMillis();
        modelTestDao.addPc(modelTestPc);
        List<String> l = xypjRemoteBaseService.selectBatchExport(modelTestPc.getMainType(), batchId);
        //modelTestDao.initPcDetailByEvalInfo(modelTestPc.getId(), modelTestPc.getModelId(), l);
        double weightAll = 0;

        // 证照指标
        List<Map<String, String>> listZz = modelTestDao.selectZzIndexesByModel(modelTestPc.getModelId());
        for (int i = 0; i < listZz.size(); i++) {
            String indexCatModelId = listZz.get(i).get("indexCatModelId");
            String inputType = listZz.get(i).get("inputType");
            double weightIndex = Double.parseDouble(listZz.get(i).get("weight"));
            String pId = listZz.get(i).get("pId");
            weightIndex = weightIndex / 100;
            weightIndex = new BigDecimal(weightIndex).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            double weight = getWeight(weightIndex, modelTestPc.getModelId(), pId, "category");
            if(scoreType.equals("2")){
            	weight=1.0;
            }
            weightAll += weight;
            String type = listZz.get(i).get("type");
            List<Map> rulesSfyzz = modelBaseDao.getRuleListByIndexModelCatForZz(listZz.get(i).get("indexCatModelId"),
                    "SFYZZ");
            List<Map> rulesZzsfdq = modelBaseDao.getRuleListByIndexModelCatForZz(listZz.get(i).get("indexCatModelId"),
                    "ZZSFDQ");
            double rate = 1;
            if (rulesSfyzz != null && rulesSfyzz.size() > 0 && rulesZzsfdq != null && rulesZzsfdq.size() > 0) {
                rate = 0.5;
            }
            double scoreTrue = 0;
            double scoreFalse = 0;
            if (rulesSfyzz != null && rulesSfyzz.size() > 0) {
                for (int j = 0; j < rulesSfyzz.size(); j++) {
                    if (rulesSfyzz.get(j).get("start").equals("1")) {
                        scoreTrue = Double.parseDouble(String.valueOf(rulesSfyzz.get(j).get("score")));
                        continue;
                    }
                    if (rulesSfyzz.get(j).get("start").equals("2")) {
                        scoreFalse = Double.parseDouble(String.valueOf(rulesSfyzz.get(j).get("score")));
                        continue;
                    }
                }
            }

            Map<String, Object> cardsMap = new HashMap<>();
            cardsMap.put("type", inputType);
            cardsMap.put("rate", rate);
            cardsMap.put("pcId", modelTestPc.getId());
            cardsMap.put("indexCatModelId", indexCatModelId);
            cardsMap.put("weight", weight);
            cardsMap.put("listSelected", l);

            cardsMap.put("scoreTrue", scoreTrue);
            cardsMap.put("scoreFalse", scoreFalse);
            modelTestDao.insertCardsSfyzzAll(cardsMap);
            if (rulesZzsfdq != null && rulesZzsfdq.size() > 0) {
                for (int j = 0; j < rulesZzsfdq.size(); j++) {
                    if (rulesZzsfdq.get(j).get("start").equals("1")) {
                        scoreTrue = Double.parseDouble(String.valueOf(rulesZzsfdq.get(j).get("score")));
                        continue;
                    }
                    if (rulesZzsfdq.get(j).get("start").equals("2")) {
                        scoreFalse = Double.parseDouble(String.valueOf(rulesZzsfdq.get(j).get("score")));
                        continue;
                    }
                }
            }

            modelTestDao.insertCardsZzsfdqAll(cardsMap);
        }

        // sql的指标
        List<Map<String, String>> listSql = modelTestDao.selectSqlIndexesByModel(modelTestPc.getModelId());
        for (int i = 0; i < listSql.size(); i++) {
            String sql = listSql.get(i).get("callContent");
            String indexCatModelId = listSql.get(i).get("indexCatModelId");
            String inputType = listSql.get(i).get("inputType");
            double weightIndex = Double.parseDouble(listSql.get(i).get("weight"));
            String pId = listSql.get(i).get("pId");
            weightIndex = weightIndex / 100;
            weightIndex = new BigDecimal(weightIndex).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            double weight = getWeight(weightIndex, modelTestPc.getModelId(), pId, "category");
            if(scoreType.equals("2")){
            	weight=1.0;
            }
            weightAll += weight;
            List<Map> listV = xypjRemoteBaseService.autoScoreByIndexSql(modelTestPc.getMainType(), sql, "", batchId);
            if (listV != null && listV.size() > 0) {
                modelTestDao.initIndexInfo(modelTestPc.getId(), indexCatModelId, listV);
            }
            if (StringUtils.isNotBlank(inputType) && "2".equals(inputType)) {
                modelTestDao.updateIndexScoreForDict(modelTestPc.getId(), indexCatModelId, weight);
            } else {
                modelTestDao.updateIndexScore(modelTestPc.getId(), indexCatModelId, weight);
            }

        }
        // 财务指标
        List<Map<String, String>> listFinance = modelTestDao.selectFinanceIndexesByModel(modelTestPc.getModelId());
        for (int i = 0; i < listFinance.size(); i++) {
            String field = listSql.get(i).get("treeElem");
            String indexCatModelId = listSql.get(i).get("indexCatModelId");
            String inputType = listSql.get(i).get("inputType");
            double weightIndex = Double.parseDouble(listSql.get(i).get("weight"));
            String pId = listSql.get(i).get("pId");
            weightIndex = weightIndex / 100;
            weightIndex = new BigDecimal(weightIndex).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            double weight = getWeight(weightIndex, modelTestPc.getModelId(), pId, "category");
            if(scoreType.equals("2")){
            	weight=1.0;
            }
            weightAll += weight;
            Map<String, Object> finanxceMap = new HashMap<>();
            finanxceMap.put("field", field);
            finanxceMap.put("pcId", modelTestPc.getId());
            finanxceMap.put("indexCatModelId", indexCatModelId);
            finanxceMap.put("weight", weight);
            finanxceMap.put("listSelected", l);
            modelTestDao.insertFinance(finanxceMap);
            if (StringUtils.isNotBlank(inputType) && "2".equals(inputType)) {
                modelTestDao.updateIndexScoreForDict(modelTestPc.getId(), indexCatModelId, weight);
            } else {
                modelTestDao.updateIndexScore(modelTestPc.getId(), indexCatModelId, weight);
            }

        }
        double leftWeight = 1 - weightAll;
        if(scoreType.equals("2")){
        	leftWeight=0;
        }
        List<Map> listAll = xypjRemoteBaseService.getAllEnterprise(modelTestPc.getMainType(), "", batchId);
        modelTestDao.initPcDetailByModelTest(modelTestPc.getId(), listAll);
        modelTestDao.deleForUserReport(modelTestPc.getId());
        modelTestDao.updatePcDetailForScore(modelTestPc.getId(), modelTestPc.getInitIndexScore(), leftWeight);
        modelTestDao.updateBgbhByPc(modelTestPc.getId());
        modelTestDao.updatePcDetailRank(modelTestPc.getId(), modelTestPc.getModelId());
        long end = System.currentTimeMillis();
        modelTestDao.updatePcExecuteTime(String.valueOf(end - start), modelTestPc.getId());

        String pcId = modelTestPc.getId();
        String pch = modelTestDao.getpchByPcid(pcId);
   	 List<Map<String, Object>> rep = new ArrayList<>();
        String table = "";
        String tablePc = "";
        try {
            long start1 = System.currentTimeMillis();
            List<Map<String, String>> listE = new ArrayList<Map<String, String>>();
            if (pch.indexOf("M") > -1) {
                table = "xypj_model_test_detail";
                tablePc = "xypj_model_test_pc";
            } else {
                table = "xypj_score_running_detail";
                tablePc = "xypj_model_test_pc";
            }
            listE = reportGetService.selectEnterpriseByPc(table, pcId);
            reportGetService.updateReportStatus(tablePc, "1", pcId);
            reportGetService.updateReportStatusBypcId(table, "1", pcId);
//            String url = pdfForXypjPagePathYz ;
//            for (int i = 0; i < listE.size(); i++) {
//                Map<String, String> pcDetail = reportGetService.selectPcDetail(table, listE.get(i).get("tybm"), pcId);
//                String bgbh = pcDetail.get("bgbh");
//                String score = String.valueOf(pcDetail.get("score"));
//                String rank = pcDetail.get("rank");
//                String evalInfo = pcDetail.get("bgbh");
//                url = url + "?tybm=" + listE.get(i).get("tybm") + "&pcId=" + pcId + "&bgbh=" + bgbh + "&score=" + score + "&rank=" + rank + "&evalInfo=" + evalInfo+"&qymc=" + listE.get(i).get("mainName") ;
//                boolean googleConvent = HtmlToPdfUtils.googleConventSync(pdfJsPath,
//                        url,
//                        absolutepath + ResourcePath.CREDIT_REPORT_DIR + pch, listE.get(i).get("mainName") + ".pdf", null, false,
//                        googlePath);
//            }
//            long end1 = System.currentTimeMillis();
//            System.out.println(end1 - start1);
//            FileUtils.compressedFile(absolutepath + ResourcePath.CREDIT_REPORT_DIR + pch, absolutepath + ResourcePath.CREDIT_REPORT_DIR_ZIP);
//            long stop = System.currentTimeMillis();
//            System.out.println(stop - end1);
            reportGetService.updateReportStatus(tablePc, "2", pcId);
            reportGetService.updateReportStatusBypcId(table, "2", pcId);
        } catch (Exception e) {
            reportGetService.updateReportStatus(tablePc, "3", pcId);
            reportGetService.updateReportStatusBypcId(table, "3", pcId);
        }
    }

    public double getWeight(double weight, String modelId, String indexCatModelId, String type) {
        Map<String, String> m = modelTestDao.getParentWeight(modelId, indexCatModelId, type);
        weight = Double.parseDouble(m.get("weight")) * weight / 100;
        weight = new BigDecimal(weight).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        type = "category";
        if (String.valueOf(m.get("pId")).equals("0") || String.valueOf(m.get("pId")).equals("")) {
            return weight;
        } else {
            return getWeight(weight, modelId, String.valueOf(m.get("pId")), type);
        }
    }

    @Override
    public List<Map<String, String>> selectModel(String zone, String industry, String reportType, String mainType) {
        // TODO Auto-generated method stub
        return modelTestDao.selectModel(zone, industry, reportType, mainType);
    }

    @Override
    public Map<String, String> selectScoreInfoByTybmAndPcid(String pcId, String tybm) {
        // TODO Auto-generated method stub
        return modelTestDao.selectScoreInfoByTybmAndPcid(pcId, tybm);
    }

    /**
     * 上传文件
     */

    @Override

    public String doUploadFile(MultipartFile file, SysUser userInfo, String absolutepath)
            throws ServiceException, Exception {

        String batchId = UuidUtil.getInstance().getUUid();
        // 文件存储路径
        StringBuilder pathFile = new StringBuilder(200);
        pathFile.append(File.separator);
        pathFile.append(userInfo.getId());
        pathFile.append(File.separator);
        pathFile.append(batchId);
        // 获取文件的后缀
        String fileType = null;
        if (file != null) {
            // 获取文件后缀
            if (file.getOriginalFilename().contains(".")) {
                fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."),
                        file.getOriginalFilename().length());
            }
        } else {
            throw new ServiceException("上传的文件不得为空");// 抛出异常
        }
        if (!".xls".equalsIgnoreCase(fileType) && !".xlsx".equalsIgnoreCase(fileType)) {
            throw new ServiceException("上传的文件格式不支持。");
        }
        // 先存储文件
        fileUpload(file, absolutepath + "/upload" + pathFile.toString(), batchId, fileType);

        Sheet st = readExcelFileForSheet(pathFile.toString());

        List<Column> columns = new ArrayList<Column>();
        Column c1 = new Column();
        c1.setMetricName("企业名称");
        Column c2 = new Column();
        c2.setMetricName("统一社会信用代码");
        columns.add(c1);
        columns.add(c2);
        Map<String, Object> map = readExcelFileHead(st, columns);
        List<String> listTybms = new ArrayList<String>();
        if (map == null || map.isEmpty()) {
            readExcelData(st, batchId);
            listTybms = modelTestDao.selectBatchExport("100000", batchId);
        } else {
            throw new RuntimeException((String) map.get("ERROR_MESS"));
        }
        return listTybms.toString();

    }

    /**
     * 上传文件
     *
     * @param file        文件
     * @param request
     * @param orgBm       所属编码
     * @param fileName    文件名称
     * @param tableNameEn 文件英文名
     * @throws IOException
     * @throws IllegalStateException
     */
    private void fileUpload(MultipartFile file, String pathFile, String batchId, String fileType)
            throws IllegalStateException, IOException {
        File uploadFile = null;

        File dirPath = new File(pathFile);
        uploadFile = new File(pathFile + File.separator + batchId + fileType);
        if (!dirPath.exists()) {
            uploadFile.mkdirs();
        }
        file.transferTo(uploadFile);

    }

    public Sheet readExcelFileForSheet(String filePath) throws IOException {
        Workbook wb = null;
        FileInputStream fs = new FileInputStream(filePath);
        // Biff8EncryptionKey.setCurrentUserPassword("AAABBBABABA{");

        if (filePath.endsWith(".xls")) {
            wb = new HSSFWorkbook(fs);
        }
        if (filePath.endsWith(".xlsx")) {
            wb = new XSSFWorkbook(fs);
        }

        Sheet st = wb.getSheetAt(0);

        fs.close();

        return st;
    }

    public Map<String, Object> readExcelFileHead(Sheet st, List<Column> columns) {
        Map<String, Object> map = null;
        try {
            Row row = null;
            // 获取校验头
            row = st.getRow(0);
            for (int i = 0; i < columns.size(); i++) {

                if (!row.getCell(i).toString().trim().equals(columns.get(i).getMetricName())) {
                    /*
                     * LOG.debug("EXCEL第{}列表头:{}", i, row.getCell(i));
                     * LOG.debug("数据库配置 第{}列表头:{}", i,
                     * columns.get(i).getMetricName());
                     * LOG.info("模板表头已被更改,请用规定的模板上传数据。");
                     */
                    map = new HashMap<String, Object>();
                    map.put("ERROR_MESS", "模板表头已被更改,请用规定的模板上传数据。");
                    break;
                }

            }
            return map;

        } catch (EncryptedDocumentException e) {
            map = new HashMap<String, Object>();
            map.put("ERROR_MESS", "模板保护密码已被更改");

        } catch (Exception e) {
            map = new HashMap<String, Object>();
            map.put("ERROR_MESS", "模板表头已被更改,请用规定的模板上传数据。");
        }
        return map;
    }

    private void readExcelData(Sheet st, String batchId) throws IOException {
        int start = 0;
        int end = 0;
        List<Map<String, Object>> listDataMap = readExcelData(start, end, st);
        if (listDataMap != null && listDataMap.size() > 0) {
            modelTestDao.insertBatchExport(listDataMap, batchId);
            modelTestDao.updateBatchExportFalse(batchId);
            modelTestDao.updateBatchExportTrue(batchId);
        } else {
            throw new RuntimeException("没有匹配到企业数据！");
        }
    }

    public List<Map<String, Object>> readExcelData(int start, int end, Sheet st) {
        // 第一,二行为标题，不取
        Row row = null;
        int length = 0;
        Map<String, Object> map = null;
        Cell cell = null;
        String value = null;

        String[] cloumn = new String[]{"qymc", "tyshxydm"};
        length = st.getLastRowNum();

        if (end > length) {
            end = length;
        }
        List<Map<String, Object>> listDataMap = new LinkedList<>();
        for (int rowIndex = start + 1; rowIndex <= end; rowIndex++) {
            row = st.getRow(rowIndex);
            if (row == null) {
                continue;
            }

            short lastCellNum = row.getLastCellNum();

            boolean emptyRow = true;

            for (int cellIndex = 0; cellIndex <= lastCellNum; cellIndex++) {

                if (row.getCell(cellIndex, Row.RETURN_BLANK_AS_NULL) != null) {

                    row.getCell(cellIndex).setCellType(HSSFCell.CELL_TYPE_STRING);

                    if (!"".equals(row.getCell(cellIndex).getStringCellValue().trim())) {
                        {

                            emptyRow = false;
                            break;
                        }
                    }

                }
            }

            if (emptyRow) {

                continue;
            }
            map = new HashMap<String, Object>();
            for (int i = 0; i < lastCellNum; i++) {
                value = "";
                cell = row.getCell(i);
                if (cell != null) {
                    // 注意：一定要设成这个，否则可能会出现乱码
                    switch (cell.getCellType()) {
                        case HSSFCell.CELL_TYPE_STRING:
                            value = cell.getStringCellValue().trim();
                            break;
                        case HSSFCell.CELL_TYPE_BLANK:
                            break;

                        default:
                            value = "";
                    }
                }
                map.put(cloumn[i], value);
                map.put("rowIndex", rowIndex + 1);
            }
            /* 把数据添加到List里面 */
            listDataMap.add(map);
        }

        return listDataMap;
    }

    @Override
    public List<Map<String, Object>> selectBatchExportForPage(List<String> columns, String[] additionalColumns,
                                                              String conditionStr, Page page, String batchId) {
        List<String> conditions = SQLBuilder.createConditionSql(conditionStr);
        List<Map<String, Object>> batchLogs = modelTestDao.selectBatchExportForPage(columns, additionalColumns,
                conditions, page, batchId);

        return batchLogs;
    }

    @Override
    public int selectNoRepeatCount(String mainType, String batchId) {
        // TODO Auto-generated method stub
        return modelTestDao.selectNoRepeatCount(mainType, batchId);
    }

    @Override
    public int selectRightAllCount(String mainType, String batchId) {
        // TODO Auto-generated method stub
        return modelTestDao.selectRightAllCount(mainType, batchId);
    }

    @Override
    public int selectErrorCount(String mainType, String batchId) {
        // TODO Auto-generated method stub
        return modelTestDao.selectErrorCount(mainType, batchId);
    }

    @Override
    public int updateReportStatus(String status, String pcId) {
        // TODO Auto-generated method stub
        return modelTestDao.updateReportStatus(status, pcId);
    }

    @Override
    public String getEvailInfoScore(String tybm) {
        return modelTestDao.getEvailInfoScore(tybm);
    }

    @Override
    public Map<String, Object> getScoreDetailByPC(String batchId) {
        return modelTestDao.getScoreDetailByPC(batchId);
    }

	@Override
	public  Map<String, Object> getxydjInfo(String tybm, String pcId) {
		
		return modelTestDao.getxydjInfo(tybm,pcId);
	}

	@Override
	public String getpchByPcid(String id) {
		// TODO Auto-generated method stub
		return modelTestDao.getpchByPcid(id);
	}



    /**
     * 获取生成PDF所需要的数据
     *
     * @param pcId
     * @param tybm
     * @return
     */
    private Map<String, Object> getTemplateData(String pcId, String tybm) {
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, String> evalInfo = selectScoreInfoByTybmAndPcid(pcId, tybm);
        result.put("bgbh", org.apache.commons.lang3.StringUtils.isNotBlank(evalInfo.get("bgbh")) ? evalInfo.get("bgbh") : "-");
        result.put("rank", org.apache.commons.lang3.StringUtils.isNotBlank(evalInfo.get("rank")) ? evalInfo.get("rank") : "-");
        result.put("evalInfo", org.apache.commons.lang3.StringUtils.isNotBlank(evalInfo.get("evalInfo")) ? evalInfo.get("evalInfo") : "-");

        int honorxxCount = 0, loseCreditCount = 0;
        // 企业工商基础信息
        List<Map> qyxx = xypjRemoteBaseService.getTableValuesByTybmAndTable("t_1000000010014_lfsxnk85kf", tybm);
        if (null != qyxx && qyxx.size() > 0) {
            result.put("A000001", null != qyxx.get(0).get("A000001") ? qyxx.get(0).get("A000001") : "-");
            result.put("A000002", null != qyxx.get(0).get("A000002") ? qyxx.get(0).get("A000002") : "-");
            result.put("A000006", null != qyxx.get(0).get("A000006") ? qyxx.get(0).get("A000006") : "-");
            result.put("djjg", null != qyxx.get(0).get("DJJG") ? qyxx.get(0).get("DJJG") : "-");
            result.put("zs", null != qyxx.get(0).get("ZS") ? qyxx.get(0).get("ZS") : "-");
            result.put("zczb", null != qyxx.get(0).get("ZCZB") ? qyxx.get(0).get("ZCZB") : "-");
            result.put("fddbr", null != qyxx.get(0).get("fddbr") ? qyxx.get(0).get("fddbr") : "-");
            result.put("ygrs", (null != qyxx.get(0).get("ygrs") && !String.valueOf(qyxx.get(0).get("ygrs")).equals("0")) ?
                    ("目前员工数" + qyxx.get(0).get("ygrs") + "人") : "目前员工数暂未统计");
            result.put("clrq", null != qyxx.get(0).get("CLRQ") ? DateUtils.parseDateByFormat(String.valueOf(qyxx.get(0).get("CLRQ")), "yyyy-MM-dd") : "-");
            result.put("jyfw", null != qyxx.get(0).get("JYFW") ? qyxx.get(0).get("JYFW") : "-");
            result.put("hydm", null != qyxx.get(0).get("HYDM") ? qyxx.get(0).get("HYDM") : "-");
        } else {
            result.put("A000001", "-");
            result.put("A000002", "-");
            result.put("A000006", "-");
            result.put("djjg", "-");
            result.put("zs", "-");
            result.put("zczb", "-");
            result.put("fddbr", "-");
            result.put("ygrs", "-");
            result.put("clrq", "-");
            result.put("jyfw", "-");
            result.put("hydm", "-");
        }
        // 历史沿革
        List<Map> changeList = xypjRemoteBaseService.getTableValuesByTybmAndTable("t_1000000010009_L6GvGS42NY", tybm);
        if (null != changeList && changeList.size() > 0) {
            StringBuilder changeSb = new StringBuilder();
            for (int i = 0; i < changeList.size(); i++) {
                changeSb.append("<tr>");
                changeSb.append("<td>").append(i + 1).append("</td>");
                changeSb.append("<td>").append(changeList.get(i).get("BGSX")).append("</td>");
                changeSb.append("<td>").append(changeList.get(i).get("BGQNR")).append("</td>");
                changeSb.append("<td>").append(changeList.get(i).get("BGHNR")).append("</td>");
                changeSb.append("<td class='date'>").append(changeList.get(i).get("BGRQ")).append("</td>");
                changeSb.append("</tr>");
            }
            result.put("changeList", changeSb.toString());
        } else {
            result.put("changeList", "<tr><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>");
        }

        // 董事会主要成员
        List<Map> mainList = xypjRemoteBaseService.getTableValuesByTybmAndTable("t_1000000010012_7AnbGEhbmt", tybm);
        if (null != mainList && mainList.size() > 0) {
            StringBuilder mainSb = new StringBuilder();
            for (int i = 0; i < mainList.size(); i++) {
                mainSb.append("<tr>");
                mainSb.append("<td>").append(i + 1).append("</td>");
                mainSb.append("<td>").append(mainList.get(i).get("XM")).append("</td>");
                mainSb.append("<td>").append(mainList.get(i).get("ZW")).append("</td>");
                mainSb.append("</tr>");
            }
            result.put("mainList", mainSb.toString());
        } else {
            result.put("mainList", "<tr><td>-</td><td>-</td><td>-</td></tr>");
        }

        // 股东信息
        List<Map> holderList = xypjRemoteBaseService.getTableValuesByTybmAndTable("t_1000000010013_8hPE5lWX7N", tybm);
        if (null != holderList && holderList.size() > 0) {
            StringBuilder holderSb = new StringBuilder();
            for (int i = 0; i < holderList.size(); i++) {
                holderSb.append("<tr>");
                holderSb.append("<td>").append(i + 1).append("</td>");
                holderSb.append("<td>").append(holderList.get(i).get("DP")).append("</td>");
                holderSb.append("<td>").append(holderList.get(i).get("SHRATIO")).append("</td>");
                holderSb.append("</tr>");
            }
            result.put("holderList", holderSb.toString());
        } else {
            result.put("holderList", "<tr><td>-</td><td>-</td><td>-</td></tr>");
        }

        // 分支机构
        List<Map> branchList = xypjRemoteBaseService.getTableValuesByTybmAndTable("t_1000000010010_aiPIZu8BZF", tybm);
        if (null != branchList && branchList.size() > 0) {
            StringBuilder branchSb = new StringBuilder();
            for (int i = 0; i < branchList.size(); i++) {
                branchSb.append("<tr>");
                branchSb.append("<td>").append(i + 1).append("</td>");
                branchSb.append("<td>").append(branchList.get(i).get("QYMC")).append("</td>");
                branchSb.append("<td>").append(branchList.get(i).get("TYXYDM")).append("</td>");
                branchSb.append("<td>").append(branchList.get(i).get("FZR")).append("</td>");
                branchSb.append("<td>").append(branchList.get(i).get("ZS")).append("</td>");
                branchSb.append("</tr>");
            }
            result.put("branchList", branchSb.toString());
        } else {
            result.put("branchList", "<tr><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>");
        }

        // 企业对外投资
        List<Map> enterpriseForeignInvestmentList = xypjRemoteBaseService.getTableValuesByTybmAndTable("t_10000020035_31QqrjNQMI", tybm);
        if (null != enterpriseForeignInvestmentList && enterpriseForeignInvestmentList.size() > 0) {
            StringBuilder efiSb = new StringBuilder();
            for (int i = 0; i < enterpriseForeignInvestmentList.size(); i++) {
                efiSb.append("<tr>");
                efiSb.append("<td>").append(i + 1).append("</td>");
                efiSb.append("<td>").append(enterpriseForeignInvestmentList.get(i).get("btzqymc")).append("</td>");
                efiSb.append("<td>").append(enterpriseForeignInvestmentList.get(i).get("czfs")).append("</td>");
                efiSb.append("<td>").append(enterpriseForeignInvestmentList.get(i).get("czbl")).append("</td>");
                efiSb.append("<td>").append(enterpriseForeignInvestmentList.get(i).get("qyzt")).append("</td>");
                efiSb.append("</tr>");
            }
            result.put("enterpriseForeignInvestmentList", efiSb.toString());
        } else {
            result.put("enterpriseForeignInvestmentList", "<tr><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>");
        }

        // 法人对外投资
        List<Map> frForeignInvestmentList = xypjRemoteBaseService.getTableValuesByTybmAndTable("t_10000020038_CdPdYlgJIU", tybm);
        if (null != frForeignInvestmentList && frForeignInvestmentList.size() > 0) {
            StringBuilder ffiSb = new StringBuilder();
            for (int i = 0; i < frForeignInvestmentList.size(); i++) {
                ffiSb.append("<tr>");
                ffiSb.append("<td>").append(i + 1).append("</td>");
                ffiSb.append("<td>").append(frForeignInvestmentList.get(i).get("frdwtzqymc")).append("</td>");
                ffiSb.append("<td>").append(frForeignInvestmentList.get(i).get("czfs")).append("</td>");
                ffiSb.append("<td>").append(frForeignInvestmentList.get(i).get("czbl")).append("</td>");
                ffiSb.append("<td>").append(frForeignInvestmentList.get(i).get("qyzt")).append("</td>");
                ffiSb.append("</tr>");
            }
            result.put("frForeignInvestmentList", ffiSb.toString());
        } else {
            result.put("frForeignInvestmentList", "<tr><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>");
        }

        // 网站备案
        List<Map> webList = xypjRemoteBaseService.getTableValuesByTybmAndTable("t_10000020067_zwmkA93VJk", tybm);
        if (null != webList && webList.size() > 0) {
            StringBuilder webSb = new StringBuilder();
            for (int i = 0; i < webList.size(); i++) {
                webSb.append("<tr>");
                webSb.append("<td>").append(i + 1).append("</td>");
                webSb.append("<td>").append(webList.get(i).get("wz")).append("</td>");
                webSb.append("<td>").append(webList.get(i).get("ym")).append("</td>");
                webSb.append("<td>").append(webList.get(i).get("xkz")).append("</td>");
                webSb.append("<td class='date'>").append(webList.get(i).get("jcsj")).append("</td>");
                webSb.append("</tr>");
            }
            result.put("webList", webSb.toString());
        } else {
            result.put("webList", "<tr><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>");
        }

        // 执行公告
        List<Map> executiveAnnouncementList = xypjRemoteBaseService.getTableValuesByTybmAndTable("t_10000020055_ooWilKgE1a", tybm);
        if (null != executiveAnnouncementList && executiveAnnouncementList.size() > 0) {
            loseCreditCount += executiveAnnouncementList.size();
            StringBuilder eaSb = new StringBuilder();
            for (int i = 0; i < executiveAnnouncementList.size(); i++) {
                eaSb.append("<tr>");
                eaSb.append("<td>").append(i + 1).append("</td>");
                eaSb.append("<td>").append(executiveAnnouncementList.get(i).get("ah")).append("</td>");
                eaSb.append("<td>").append(executiveAnnouncementList.get(i).get("ajzt")).append("</td>");
                eaSb.append("<td class='date'>").append(executiveAnnouncementList.get(i).get("lasj")).append("</td>");
                eaSb.append("<td>").append(executiveAnnouncementList.get(i).get("fymc")).append("</td>");
                eaSb.append("</tr>");
            }
            result.put("executiveAnnouncementList", eaSb.toString());
        } else {
            result.put("executiveAnnouncementList", "<tr><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>");
        }

        // 法院公告
        List<Map> courtAnnouncementList = xypjRemoteBaseService.getTableValuesByTybmAndTable("t_10000020056_FqHbBIP5wW", tybm);
        if (null != courtAnnouncementList && courtAnnouncementList.size() > 0) {
            loseCreditCount += courtAnnouncementList.size();
            StringBuilder eaSb = new StringBuilder();
            for (int i = 0; i < courtAnnouncementList.size(); i++) {
                eaSb.append("<tr>");
                eaSb.append("<td>").append(i + 1).append("</td>");
                eaSb.append("<td>").append(courtAnnouncementList.get(i).get("xm")).append("</td>");
                eaSb.append("<td>").append(courtAnnouncementList.get(i).get("gglx")).append("</td>");
                eaSb.append("<td class='date'>").append(courtAnnouncementList.get(i).get("fbsj")).append("</td>");
                eaSb.append("<td>").append(courtAnnouncementList.get(i).get("fymc")).append("</td>");
                eaSb.append("</tr>");
            }
            result.put("courtAnnouncementList", eaSb.toString());
        } else {
            result.put("courtAnnouncementList", "<tr><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>");
        }

        // 裁判文书
        List<Map> referBookList = xypjRemoteBaseService.getTableValuesByTybmAndTable("t_10000020057_VqVvAlMpeL", tybm);
        if (null != referBookList && referBookList.size() > 0) {
            loseCreditCount += referBookList.size();
            StringBuilder referBookSb = new StringBuilder();
            for (int i = 0; i < referBookList.size(); i++) {
                referBookSb.append("<tr>");
                referBookSb.append("<td>").append(i + 1).append("</td>");
                referBookSb.append("<td>").append(referBookList.get(i).get("ah")).append("</td>");
                referBookSb.append("<td>").append(referBookList.get(i).get("ay")).append("</td>");
                referBookSb.append("<td>").append(referBookList.get(i).get("wslx")).append("</td>");
                referBookSb.append("<td class='date'>").append(referBookList.get(i).get("sjsj")).append("</td>");
                referBookSb.append("</tr>");
            }
            result.put("referBookList", referBookSb.toString());
        } else {
            result.put("referBookList", "<tr><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>");
        }

        // 开庭公告
        List<Map> announcementCourtSessionList = xypjRemoteBaseService.getTableValuesByTybmAndTable("t_10000020058_NdGY8UTaWV", tybm);
        if (null != announcementCourtSessionList && announcementCourtSessionList.size() > 0) {
            loseCreditCount += announcementCourtSessionList.size();
            StringBuilder referBookSb = new StringBuilder();
            for (int i = 0; i < announcementCourtSessionList.size(); i++) {
                referBookSb.append("<tr>");
                referBookSb.append("<td>").append(i + 1).append("</td>");
                referBookSb.append("<td>").append(announcementCourtSessionList.get(i).get("ah")).append("</td>");
                referBookSb.append("<td class='date'>").append(announcementCourtSessionList.get(i).get("ktsj")).append("</td>");
                referBookSb.append("<td>").append(announcementCourtSessionList.get(i).get("fymc")).append("</td>");
                referBookSb.append("</tr>");
            }
            result.put("announcementCourtSessionList", referBookSb.toString());
        } else {
            result.put("announcementCourtSessionList", "<tr><td>-</td><td>-</td><td>-</td><td>-</td></tr>");
        }

        // 欠税公告
        List<Map> taxArrearList = xypjRemoteBaseService.getTableValuesByTybmAndTable("t_10000020068_omagh7XEue", tybm);
        if (null != taxArrearList && taxArrearList.size() > 0) {
            loseCreditCount += taxArrearList.size();
            StringBuilder taxArrearSb = new StringBuilder();
            for (int i = 0; i < taxArrearList.size(); i++) {
                taxArrearSb.append("<tr>");
                taxArrearSb.append("<td>").append(i + 1).append("</td>");
                taxArrearSb.append("<td>").append(taxArrearList.get(i).get("nsrmc")).append("</td>");
                taxArrearSb.append("<td>").append(taxArrearList.get(i).get("lx")).append("</td>");
                taxArrearSb.append("<td>").append(taxArrearList.get(i).get("qssz")).append("</td>");
                taxArrearSb.append("<td>").append(taxArrearList.get(i).get("qsjr")).append("</td>");
                taxArrearSb.append("</tr>");
            }
            result.put("taxArrearList", taxArrearSb.toString());
        } else {
            result.put("taxArrearList", "<tr><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>");
        }

        // 行政处罚
        List<Map> adPenaltyList = xypjRemoteBaseService.getTableValuesByTybmAndTable("t_10000020047_4z8Y6U03BN", tybm);
        if (null != adPenaltyList && adPenaltyList.size() > 0) {
            loseCreditCount += adPenaltyList.size();
            StringBuilder adPenaltySb = new StringBuilder();
            for (int i = 0; i < taxArrearList.size(); i++) {
                adPenaltySb.append("<tr>");
                adPenaltySb.append("<td>").append(i + 1).append("</td>");
                adPenaltySb.append("<td>").append(adPenaltyList.get(i).get("cfjdws")).append("</td>");
                adPenaltySb.append("<td class='date'>").append(adPenaltyList.get(i).get("cfjdsqfrq")).append("</td>");
                adPenaltySb.append("<td>").append(adPenaltyList.get(i).get("zywfss")).append("</td>");
                adPenaltySb.append("<td>").append(adPenaltyList.get(i).get("cfje")).append("</td>");
                adPenaltySb.append("<td>").append(adPenaltyList.get(i).get("zcxzcfjdjg")).append("</td>");
                adPenaltySb.append("</tr>");
            }
            result.put("adPenaltyList", adPenaltySb.toString());
        } else {
            result.put("adPenaltyList", "<tr><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>");
        }

        // 失信被执行人
        List<Map> loseCreditList = xypjRemoteBaseService.getTableValuesByTybmAndTable("t_10000020053_ixye65DjaA", tybm);
        if (null != loseCreditList && loseCreditList.size() > 0) {
            loseCreditCount += loseCreditList.size();
            StringBuilder loseCreditSb = new StringBuilder();
            for (int i = 0; i < loseCreditList.size(); i++) {
                loseCreditSb.append("<tr>");
                loseCreditSb.append("<td>").append(i + 1).append("</td>");
                loseCreditSb.append("<td>").append(loseCreditList.get(i).get("ah")).append("</td>");
                loseCreditSb.append("<td>").append(loseCreditList.get(i).get("bzxrxm")).append("</td>");
                loseCreditSb.append("<td class='date'>").append(loseCreditList.get(i).get("lasj")).append("</td>");
                loseCreditSb.append("<td>").append(loseCreditList.get(i).get("bzxrdlqk")).append("</td>");
                loseCreditSb.append("<td>").append(loseCreditList.get(i).get("zxfy")).append("</td>");
                loseCreditSb.append("</tr>");
            }
            result.put("loseCreditList", loseCreditSb.toString());
        } else {
            result.put("loseCreditList", "<tr><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>");
        }

        // 商标注册
        List<Map> logoList = xypjRemoteBaseService.getTableValuesByTybmAndTable("t_10000020064_q7zexUMvJF", tybm);
        if (null != logoList && logoList.size() > 0) {
            honorxxCount += logoList.size();
            StringBuilder logoSb = new StringBuilder();
            for (int i = 0; i < logoList.size(); i++) {
                logoSb.append("<tr>");
                logoSb.append("<td>").append(i + 1).append("</td>");
                logoSb.append("<td>").append(logoList.get(i).get("zch")).append("</td>");
                logoSb.append("<td>").append(logoList.get(i).get("sbmc")).append("</td>");
                logoSb.append("<td>").append(logoList.get(i).get("lb")).append("</td>");
                logoSb.append("<td>").append(logoList.get(i).get("zt")).append("</td>");
                logoSb.append("<td class='date'>").append(logoList.get(i).get("sqrq")).append("</td>");
                logoSb.append("</tr>");
            }
            result.put("logoList", logoSb.toString());
            result.put("zzxx", "●企业目前获取" + logoList.size() + "个企业资质信息");
        } else {
            result.put("logoList", "<tr><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>");
            result.put("zzxx", "●企业目前未获取任何企业资质信息");
        }

        // 知识产权
        List<Map> intellectualPropertyRightList = xypjRemoteBaseService.getTableValuesByTybmAndTable("t_10000020065_7ZTo0ASgjr", tybm);
        if (null != intellectualPropertyRightList && intellectualPropertyRightList.size() > 0) {
            honorxxCount += intellectualPropertyRightList.size();
            StringBuilder iprSb = new StringBuilder();
            for (int i = 0; i < intellectualPropertyRightList.size(); i++) {
                iprSb.append("<tr>");
                iprSb.append("<td>").append(i + 1).append("</td>");
                iprSb.append("<td>").append(intellectualPropertyRightList.get(i).get("sqhzlh")).append("</td>");
                iprSb.append("<td>").append(intellectualPropertyRightList.get(i).get("zlmc")).append("</td>");
                iprSb.append("<td>").append(intellectualPropertyRightList.get(i).get("zllx")).append("</td>");
                iprSb.append("<td class='date'>").append(intellectualPropertyRightList.get(i).get("sqrq")).append("</td>");
                iprSb.append("</tr>");
            }
            result.put("intellectualPropertyRightList", iprSb.toString());
        } else {
            result.put("intellectualPropertyRightList", "<tr><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>");
        }

        // 税务评级
        List<Map> taxRankList = xypjRemoteBaseService.getTableValuesByTybmAndTable("t_10000020066_m4N6fK4BDU", tybm);
        if (null != taxRankList && taxRankList.size() > 0) {
            honorxxCount += taxRankList.size();
            StringBuilder taxRankSb = new StringBuilder();
            for (int i = 0; i < intellectualPropertyRightList.size(); i++) {
                taxRankSb.append("<tr>");
                taxRankSb.append("<td>").append(i + 1).append("</td>");
                taxRankSb.append("<td>").append(taxRankList.get(i).get("nf")).append("</td>");
                taxRankSb.append("<td>").append(taxRankList.get(i).get("nsdj")).append("</td>");
                taxRankSb.append("<td>").append(taxRankList.get(i).get("lx")).append("</td>");
                taxRankSb.append("<td>").append(taxRankList.get(i).get("pjdw")).append("</td>");
                taxRankSb.append("</tr>");
            }
            result.put("taxRankList", taxRankSb.toString());
        } else {
            result.put("taxRankList", "<tr><td>-</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>");
        }

        if (honorxxCount <= 0) {
            result.put("honorxx", "●企业目前未获取任何企业荣誉信息");
        } else {
            result.put("honorxx", "●企业目前获取" + honorxxCount + "个企业荣誉信息");
        }

        if (loseCreditCount <= 0) {
            result.put("losexx", "●经公共信用信息中心数据库查询，该公司在近三年内无失信信息记录");
        } else {
            result.put("losexx", "●经公共信用信息中心数据库查询，该公司在近三年内有" + loseCreditCount + "条失信信息记录");
        }

        result.put("nowTime", DateUtils.getStringOfTodayDate("yyyy年MM月dd日"));
        ReportCustom latest = customService.getLatest();

        result.put("addressBg", "报告出具部门：" + latest.getDept());
        result.put("reportWatermark", latest.getReportWatermark());
        result.put("enterpriseIcon", latest.getEnterpriseIcon());
        return result;
    }

    private Map<String, Object> getTemplateData(String tybm, String pcId, String qymc, String absolutepath) {
        try {
            Map<String, Object> result = new HashMap<>();

            Map<String, String> params = new HashMap<String, String>(1);
            params.put("qymc", qymc);
            params.put("tybm", tybm);
            String url = urlHelper.getPlatformServerUrl() + ApiPath.REQUEST_SELECT_INFO_DETAIL;
            RestServiceResult<Map<String, Object>> rsrMap = null;
            try {
                // 发送请求
                String resultStr = HttpAgent.getInstance().sendHttpPost(url, params, null);
                if (StringUtils.isNotBlank(resultStr)) {
                    rsrMap = JSONObject.parseObject(resultStr, new TypeReference<RestServiceResult<Map<String, Object>>>() {
                    });
                } else {
                    return null;
                }
            } catch (Exception e) {
                RestServiceResult<String> data = new RestServiceResult<String>();
                data.setCode(RestServiceResult.CODE_UNKNOW);
                data.setMsg("系统异常，请稍后再试！");
                ExceptionLogger.error("远程服务-表单校验异常！(XypjRemoteBaseService#checkTable) URL:" + url
                        + " Params:" + params.toString() + "Result:" + result, e.getMessage(), e);
            }

            if (null == rsrMap.getData()) {
                return null;
            }

            Map<String, Object> data = rsrMap.getData();


            List<Map> jbxxList = (List<Map>) data.get("基本信息");
            List<Map> gsdjxxList = (List<Map>) ((Map) jbxxList.get(0)).get("工商登记信息");
            if (null != jbxxList && jbxxList.size() > 0 && null != gsdjxxList && gsdjxxList.size() > 0) {
                Map gsdjxx = gsdjxxList.get(0);
                result.put("A000001", isNull(gsdjxx.get("A000001")));
                result.put("A000002", isNull(gsdjxx.get("A000002")));
                result.put("dz", isNull(gsdjxx.get("DZ")));
                result.put("fddbr", isNull(gsdjxx.get("FDDBR")));
                result.put("qylx", isNull(gsdjxx.get("qylx")));
                result.put("clrq", formatDate(gsdjxx.get("CLRQ")));
                result.put("jyfw", isNull(gsdjxx.get("JYFW")));
            } else {
                result.put("A000001", "-");
                result.put("A000002", "-");
                result.put("dz", "-");
                result.put("fddbr", "-");
                result.put("qylx", "-");
                result.put("clrq", "-");
                result.put("jyfw", "-");
            }
            List<Map> xzxkList = (List<Map>) data.get("行政许可");
            if (null != xzxkList && xzxkList.size() > 0) {
                Map frxzxkMap = xzxkList.get(0);
                int xzxkDataCount = 0;
                if (null != frxzxkMap && Integer.valueOf(String.valueOf(frxzxkMap.get("size"))).intValue() > 0) {
                    List<Map> frxzxk = (List<Map>) frxzxkMap.get("法人行政许可");
                    xzxkDataCount = Integer.valueOf(String.valueOf(frxzxkMap.get("size"))).intValue();
                    StringBuilder xzxkDataDom = new StringBuilder();
                    for (int i = 0; i < frxzxk.size(); i++) {
                        xzxkDataDom.append("<table><tr><th style=\"width:30%\"></th><th>第").append(i + 1).append("条</th></tr>");
                        xzxkDataDom.append("<tr><td style=\"width:30%\">行政许可决定文书号：</td><td style=\"width:70%\">").append(isNull(frxzxk.get(i).get("XK_WSH"))).append("</td></tr>");
                        xzxkDataDom.append("<tr><td>行政许可决定文书名称：</td><td>").append(isNull(frxzxk.get(i).get("XK_XKWS"))).append("</td></tr>");
                        xzxkDataDom.append("<tr><td>许可证书名称：</td><td>").append(isNull(frxzxk.get(i).get("XK_XKZS"))).append("</td></tr>");
                        xzxkDataDom.append("<tr><td>许可类别：</td><td>").append(isNull(frxzxk.get(i).get("XK_XKLB"))).append("</td></tr>");
                        xzxkDataDom.append("<tr><td>许可编号：</td><td>").append(isNull(frxzxk.get(i).get("XK_XKBH"))).append("</td></tr>");
                        xzxkDataDom.append("<tr><td>许可决定日期：</td><td>").append(formatDate(frxzxk.get(i).get("XK_JDRQ"))).append("</td></tr>");
                        xzxkDataDom.append("<tr><td>有效期自：</td><td>").append(formatDate(frxzxk.get(i).get("XK_YXQZ"))).append("</td></tr>");
                        xzxkDataDom.append("<tr><td>有效期至：</td><td>").append(formatDate(frxzxk.get(i).get("XK_YXQZI"))).append("</td></tr>");
                        xzxkDataDom.append("<tr><td>许可内容：</td><td>").append(isNull(frxzxk.get(i).get("XK_NR"))).append("</td></tr>");
                        xzxkDataDom.append("<tr><td>许可机关：</td><td>").append(isNull(frxzxk.get(i).get("XK_XKJG"))).append("</td></tr>");
                        xzxkDataDom.append("<tr><td>许可机关统一社会信用代码：</td><td>").append(isNull(frxzxk.get(i).get("XK_XKJGDM"))).append("</td></tr>");
                        xzxkDataDom.append("</table>");
                    }
                    result.put("xzxkDataDom", xzxkDataDom.toString());
                } else {
                    result.put("xzxkDataDom", "");
                }
                if (xzxkDataCount == 0)
                    result.put("xzxkDataStyle", Integer.valueOf(xzxkDataCount));
                if (xzxkDataCount == 0) {
                    result.put("xzxkDataStyle", "style=\"display: none;\"");
                } else {
                    result.put("xzxkDataStyle", "style=\"display: block;\"");
                }
                result.put("xzxkDataCount", Integer.valueOf(xzxkDataCount));
                result.put("ggxx_xzxk", xzxkDataCount + "条");
            }

            List<Map> xzcfList = (List<Map>) data.get("行政处罚");
            if (null != xzcfList && xzcfList.size() > 0) {
                Map frxzcfMap = xzcfList.get(0);
                int xzcfDataCount = 0;
                if (null != frxzcfMap && Integer.valueOf(String.valueOf(frxzcfMap.get("size"))).intValue() > 0) {
                    List<Map> frxzcf = (List<Map>) frxzcfMap.get("法人行政处罚");
                    xzcfDataCount = Integer.valueOf(String.valueOf(frxzcfMap.get("size"))).intValue();
                    StringBuilder xzcfDataDom = new StringBuilder();
                    for (int i = 0; i < frxzcf.size(); i++) {
                        xzcfDataDom.append("<table><tr><th style=\"width:30%\"></th><th >第").append((i + 1)).append("条</th></tr>");
                        xzcfDataDom.append("<tr><td style=\"width:30%\">行政处罚决定文书号：</td><td style=\"width:70%\">").append(isNull(frxzcf.get(i).get("CF_WSH"))).append("</td></tr>");
                        xzcfDataDom.append("<tr><td>违法行为类型：</td><td>").append(isNull(frxzcf.get(i).get("CF_WFXW"))).append("</td></tr>");
                        xzcfDataDom.append("<tr><td>处罚事由：</td><td>").append(isNull(frxzcf.get(i).get("CF_SY"))).append("</td></tr>");
                        xzcfDataDom.append("<tr><td>处罚类别：</td><td>").append(isNull(frxzcf.get(i).get("CF_LB"))).append("</td></tr>");
                        xzcfDataDom.append("<tr><td>处罚依据：</td><td>").append(isNull(frxzcf.get(i).get("CF_YJ"))).append("</td></tr>");
                        xzcfDataDom.append("<tr><td>处罚内容：</td><td>").append(isNull(frxzcf.get(i).get("CF_NR"))).append("</td></tr>");
                        xzcfDataDom.append("<tr><td>罚款金额（万元）：</td><td>").append(isNull(frxzcf.get(i).get("CF_NR_FK"))).append("</td></tr>");
                        xzcfDataDom.append("<tr><td>没收违法所得、没收非法财物的金额（万元）：</td><td>").append(isNull(frxzcf.get(i).get("CF_NR_WFFF"))).append("</td></tr>");
                        xzcfDataDom.append("<tr><td>暂扣或吊销证照名称及编号：</td><td>").append(isNull(frxzcf.get(i).get("CF_NR_ZKDX"))).append("</td></tr>");
                        xzcfDataDom.append("<tr><td>处罚有效期：</td><td>").append(formatDate(frxzcf.get(i).get("CF_YXQ"))).append("</td></tr>");
                        xzcfDataDom.append("<tr><td>公示截止期：</td><td>").append(formatDate(frxzcf.get(i).get("CF_GSJZQ"))).append("</td></tr>");
                        xzcfDataDom.append("<tr><td>处罚机关：</td><td>").append(isNull(frxzcf.get(i).get("CF_CFJG"))).append("</td></tr>");
                        xzcfDataDom.append("<tr><td>处罚机关统一社会信用代码：</td><td>").append(isNull(frxzcf.get(i).get("CF_CFJGDM"))).append("</td></tr>");
                        xzcfDataDom.append("<tr><td>数据来源单位：</td><td>").append(isNull(frxzcf.get(i).get("CF_SJLY"))).append("</td></tr>");
                        xzcfDataDom.append("<tr><td>数据来源单位统一社会信用代码：</td><td>").append(isNull(frxzcf.get(i).get("CF_SJLYDM"))).append("</td></tr>");
                        xzcfDataDom.append("<tr><td>备注：</td><td>").append(isNull(frxzcf.get(i).get("BZ"))).append("</td></tr>");
                        xzcfDataDom.append("</table>");
                    }
                    result.put("xzcfDataDom", xzcfDataDom.toString());
                } else {
                    result.put("xzcfDataDom", "");
                }
                if (xzcfDataCount == 0) {
                    result.put("xzcfDataStyle", "style=\"display: none;\"");
                } else {
                    result.put("xzcfDataStyle", "style=\"display: block;\"");
                }
                result.put("xzcfDataCount", Integer.valueOf(xzcfDataCount));
                result.put("ggxx_xzcf", xzcfDataCount + "条");
            }
            List<Map> sxjlList = (List<Map>) data.get("守信激励");
            if (null != sxjlList && sxjlList.size() > 0) {
                Map xsjlMap = sxjlList.get(0);
                int sxjlDataCount = 0;
                if (null != xsjlMap && Integer.valueOf(String.valueOf(xsjlMap.get("size"))).intValue() > 0) {
                    List<Map> ajnsr = (List<Map>) xsjlMap.get("A级纳税人");
                    sxjlDataCount = Integer.valueOf(String.valueOf(xsjlMap.get("size"))).intValue();
                    StringBuilder sxjlDataDom = new StringBuilder();
                    for (int i = 0; i < ajnsr.size(); i++) {
                        sxjlDataDom.append("<table><tr><th style=\"width:30%\"></th><th >第").append(i + 1).append("条</th></tr>");
                        sxjlDataDom.append("<tr><td style=\"width:30%\">认定单位：</td><td style=\"width:70%\">").append(isNull(ajnsr.get(i).get("pjswjg"))).append("</td></tr>");
                        sxjlDataDom.append("<tr><td>评价结果：</td><td>").append(isNull(ajnsr.get(i).get("pjjg"))).append("</td></tr>");
                        sxjlDataDom.append("<tr><td>评价年度：</td><td>").append(isNull(ajnsr.get(i).get("pjnd"))).append("</td></tr>");
                        sxjlDataDom.append("</table>");
                    }
                    result.put("sxjlDataDom", sxjlDataDom.toString());
                } else {
                    result.put("sxjlDataDom", "");
                }
                if (sxjlDataCount == 0) {
                    result.put("sxjlDataStyle", "style=\"display: none;\"");
                } else {
                    result.put("sxjlDataStyle", "style=\"display: block;\"");
                }
                result.put("sxjlDataCount", Integer.valueOf(sxjlDataCount));
                result.put("ggxx_sxjl", sxjlDataCount + "条");
            }
            int sxcjDataCount = 0;
            StringBuilder sxcjDataDom1 = new StringBuilder();
            StringBuilder sxcjDataDom2 = new StringBuilder();
            List<Map> sxcjList = (List<Map>) data.get("失信惩戒");
            if (null != sxcjList && sxcjList.size() > 0)
                for (int i = 0; i < sxcjList.size(); i++) {
                    Map map = sxcjList.get(i);
                    if (null != map.get("size"))
                        sxcjDataCount += Integer.valueOf(String.valueOf(map.get("size"))).intValue();
                    if (null != map.get("失信被执行人")) {
                        List<Map> sxzxrList = (List<Map>) map.get("失信被执行人");
                        for (int j = 0; j < sxzxrList.size(); j++) {
                            sxcjDataDom2.append("<table><tr><th style=\"width:30%\">失信被执行人</th><th>第").append((i + 1)).append("条</th></tr>");
                            sxcjDataDom2.append("<tr><td style=\"width:30%\">行政处罚决定书文号：</td><td style=\"width:70%\">").append(isNull(sxzxrList.get(j).get("CF_WSH"))).append("</td></tr>");
                            sxcjDataDom2.append("<tr><td>违法行为类型：</td><td>").append(isNull(sxzxrList.get(j).get("CF_WFXW"))).append("</td></tr>");
                            sxcjDataDom2.append("<tr><td>违法事实：</td><td>").append(isNull(sxzxrList.get(j).get("CF_SY"))).append("</td></tr>");
                            sxcjDataDom2.append("<tr><td>处罚依据：</td><td>").append(isNull(sxzxrList.get(j).get("ah"))).append("</td></tr>");
                            sxcjDataDom2.append("<tr><td>处罚类别：</td><td>").append(isNull(sxzxrList.get(j).get("CF_CFLB"))).append("</td></tr>");
                            sxcjDataDom2.append("<tr><td>处罚内容：</td><td>").append(isNull(sxzxrList.get(j).get("CF_NR"))).append("</td></tr>");
                            sxcjDataDom2.append("<tr><td>罚款金额（万元）：</td><td>").append(isNull(sxzxrList.get(j).get("CF_NR_FK"))).append("</td></tr>");
                            sxcjDataDom2.append("<tr><td>没收违法所得、没收非法财物的金额（万元）：</td><td>").append(isNull(sxzxrList.get(j).get("CF_NR_WFFF"))).append("</td></tr>");
                            sxcjDataDom2.append("<tr><td>暂扣或吊销证照名称及编号：</td><td>").append(isNull(sxzxrList.get(j).get("CF_NR_ZKDX"))).append("</td></tr>");
                            sxcjDataDom2.append("<tr><td>处罚决定日期：</td><td>").append(formatDate(sxzxrList.get(j).get("creditTime"))).append("</td></tr>");
                            sxcjDataDom2.append("<tr><td>处罚有效期：</td><td>").append(isNull(sxzxrList.get(j).get("CF_YXQ"))).append("</td></tr>");
                            sxcjDataDom2.append("<tr><td>公示截止期：</td><td>").append(isNull(sxzxrList.get(j).get("CF_GSJZQ"))).append("</td></tr>");
                            sxcjDataDom2.append("<tr><td>处罚机关：</td><td>").append(isNull(sxzxrList.get(j).get("zxfy"))).append("</td></tr>");
                            sxcjDataDom2.append("<tr><td>处罚机关统一社会信用代码：</td><td>").append(isNull(sxzxrList.get(j).get("CF_CFJGDM"))).append("</td></tr>");
                            sxcjDataDom2.append("<tr><td>数据来源单位：</td><td>").append(isNull(sxzxrList.get(j).get("A000001"))).append("</td></tr>");
                            sxcjDataDom2.append("<tr><td>数据来源单位统一社会信用代码：</td><td>").append(isNull(sxzxrList.get(j).get("A000002"))).append("</td></tr></table></div>");
                        }
                    }
                    if (null != map.get("重大税收违法案件")) {
                        List<Map> zdsswfList = (List<Map>) map.get("重大税收违法案件");
                        for (int j = 0; j < zdsswfList.size(); j++) {
                            sxcjDataDom1.append("<table><tr><th style=\"width:30%\">重大税收违法案件</th><th>第").append(i + 1).append("条</th></tr>");
                            sxcjDataDom1.append("<tr><td style=\"width:30%\">黑名单名称：</td><td style=\"width:70%\">").append(isNull(zdsswfList.get(j).get("A000001"))).append("</td></tr>");
                            sxcjDataDom1.append("<tr><td>列入名单事由：</td><td>").append(isNull(zdsswfList.get(j).get("zywfss"))).append("</td></tr>");
                            sxcjDataDom1.append("<tr><td>认定依据：</td><td>").append(isNull(zdsswfList.get(j).get("clcfqk"))).append("</td></tr>");
                            sxcjDataDom1.append("<tr><td>认定日期：</td><td>").append(formatDate(zdsswfList.get(j).get("larq"))).append("</td></tr>");
                            sxcjDataDom1.append("<tr><td>有效期始：</td><td>").append(isNull(zdsswfList.get(j).get("RDRQ"))).append("</td></tr>");
                            sxcjDataDom1.append("<tr><td>有效期至：</td><td>").append(isNull(zdsswfList.get(j).get("YXQZ"))).append("</td></tr>");
                            sxcjDataDom1.append("<tr><td>认定单位：</td><td>").append(isNull(zdsswfList.get(j).get("deptName"))).append("</td></tr>");
                            sxcjDataDom1.append("<tr><td>认定单位统一社会信用代码：</td><td>").append(isNull(zdsswfList.get(j).get("A000002"))).append("</td></tr></table></div>");

                        }
                    }
                }
            result.put("sxcjDataCount", Integer.valueOf(sxcjDataCount));
            if (sxcjDataCount == 0) {
                result.put("sxcjDataStyle", "style=\"display: none;\"");
            } else {
                result.put("sxcjDataStyle", "style=\"display: block;\"");
            }
            result.put("ggxx_sxcj", sxcjDataCount + "条");
            result.put("sxcjDataDom", sxcjDataDom1.append(sxcjDataDom2).toString());


            List<Map> zdgzList = (List<Map>) data.get("重点关注");
            if (null != zdgzList && zdgzList.size() > 0) {
                Map zdgzMap = zdgzList.get(0);
                int zdgzDataCount = 0;
                if (null != zdgzMap && Integer.valueOf(String.valueOf(zdgzMap.get("size"))).intValue() > 0) {
                    List<Map> jyycmlList = (List<Map>) zdgzMap.get("经营异常名录");
                    zdgzDataCount = Integer.valueOf(String.valueOf(zdgzMap.get("size"))).intValue();
                    StringBuilder zdgzDataDom = new StringBuilder();
                    for (int i = 0; i < jyycmlList.size(); i++) {


                        zdgzDataDom.append("<table><tr><th style='width:30%'></th><th>第").append(i + 1).append("条</th></tr>");
                        zdgzDataDom.append("<tr><td style='width:30%'>企业名称：</td><td style='width:70%'>").append(isNull(jyycmlList.get(i).get("A000001"))).append("</td></tr>");
                        zdgzDataDom.append("<tr><td>列入经营异常名录原因：</td><td>").append(isNull(jyycmlList.get(i).get("pjjgpjjg"))).append("</td></tr>");
                        zdgzDataDom.append("<tr><td>列入日期：</td><td>").append(formatDate(jyycmlList.get(i).get("lrrq"))).append("</td></tr>");
                        zdgzDataDom.append("<tr><td>做出决定机关(列入)：</td><td>").append(isNull(jyycmlList.get(i).get("lrjdjg"))).append("</td></tr>");
                        zdgzDataDom.append("<tr><td>移出异常名录原因：</td><td>").append(isNull(jyycmlList.get(i).get("ycyy"))).append("</td></tr>");
                        zdgzDataDom.append("<tr><td>移出日期：</td><td>").append(formatDate(jyycmlList.get(i).get("ycrq"))).append("</td></tr>");
                        zdgzDataDom.append("<tr><td>做出决定机关(移出)：</td><td>").append(isNull(jyycmlList.get(i).get("ycjdjg"))).append("</td></tr>");
                        zdgzDataDom.append("</table>");
                    }
                    result.put("zdgzDataDom", zdgzDataDom.toString());
                } else {
                    result.put("zdgzDataDom", "");
                }
                if (zdgzDataCount == 0) {
                    result.put("zdgzDataStyle", "style=\"display: none;\"");
                } else {
                    result.put("zdgzDataStyle", "style=\"display: block;\"");
                }
                result.put("zdgzDataCount", Integer.valueOf(zdgzDataCount));
                result.put("ggxx_zdgz", zdgzDataCount + "条");
            }

            ReportCustom latest = customService.getLatest();
            if (StringUtils.isNotBlank(latest.getReportWatermark())) {
                result.put("waterPath", absolutepath + latest.getReportWatermark());
            } else {
                result.put("waterPath", absolutepath + "/images/watermark/watermark.png");
            }

            if (StringUtils.isNotBlank(latest.getEnterpriseIcon())) {
                result.put("enterpriseIcon", absolutepath + latest.getEnterpriseIcon());
            } else {
                result.put("enterpriseIcon", absolutepath + "/images/watermark/icon.png");
            }


            result.put("bgbh", "XYBG" + DateUtils.getCurrentTimeNumber());
            result.put("addressBg", latest.getDept());
            result.put("title", latest.getReportStatement());
            result.put("nowTime", DateUtils.getStringOfTodayDate("yyyy-MM-dd"));
            Map<String, Object> pjxxData = getxydjInfo(tybm, pcId);

            //  守信激励
            StringBuilder pjxxDataDom = new StringBuilder();
            pjxxDataDom.append("<table>");
            pjxxDataDom.append("<tr><td style='width:30%'>分数：</td><td style='width:70%'>").append(isNull(pjxxData.get("score"))).append("</td></tr>");
            pjxxDataDom.append("<tr><td>等级：</td><td>").append(isNull(pjxxData.get("rank"))).append("</td></tr>");
            pjxxDataDom.append("<tr><td>评价日期：</td><td>").append(formatDate(pjxxData.get("cjsj"))).append("</td></tr>");
            pjxxDataDom.append("</table>");
            result.put("pjxxDataDom", pjxxDataDom);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private String isNull(Object o) {
        if (null != o)
            return String.valueOf(o);
        return "-";
    }

    private String formatDate(Object o) {
        if (null != o) {
            if (o  instanceof Long) {
                return DateUtils.getDateToString(Long.valueOf(String.valueOf(o)));
            } else {
                Date date = DateUtils.parseDateByFormat(String.valueOf(o), "yyyy-MM-dd");
                return DateUtils.dateParseShortString(date);
            }
        }
        return "-";
    }
}
